package br.com.evento.model.db;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import br.com.evento.exception.SystemException;
import br.com.evento.model.entity.Usuario;
import br.com.evento.util.DBUtil;

public class UsuarioDAO extends DAOBase<Usuario> {

	private static final long serialVersionUID = 1L;

	public UsuarioDAO(EntityManagerFactory emf) {
		super(emf);
	}

	public Usuario getUsuarioByEmail (String email, String senha) throws SystemException {
		
		EntityManager em = null;
		
		try {
			
			em = getEmf().createEntityManager();
			Query query = em.createNamedQuery(Usuario.class.getSimpleName() + ".findByEmail");
			query.setParameter("email", email);
			Usuario cliente = (Usuario) query.getSingleResult();
			
			if (cliente.getSenha().equals(senha)){
				return cliente;
			} else {
				return null;
			}
		
		} catch (NoResultException e) {
			throw e;
			
		} catch (Exception e) {
			throw new SystemException("Erro na busca.", e);
			
		} finally {
			DBUtil.closeFlux(em);
		}
	}
	
	
}
